home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / bash-1.12 / dist / examples / functions / ksh-compat < prev    next >
Encoding:
Text File  |  1991-12-31  |  2.6 KB  |  149 lines

  1. #
  2. # ksh-compat -- functions and aliases to provide the beginnings of a ksh 
  3. #            environment for bash.
  4. #
  5. # Chet Ramey
  6. # chet@ins.CWRU.Edu
  7. #
  8. #
  9. # These are definitions for the ksh compiled-in `exported aliases'.  There
  10. # are others, but we already have substitutes for them: "history", "type",
  11. # and "hash".
  12. #
  13. alias r="fc -e -"
  14. alias functions="typeset -f"
  15. alias integer="typeset -i"
  16. alias nohup="nohup "
  17. alias true=":"
  18. alias false="let 0"
  19.  
  20. #
  21. # An almost-ksh compatible `whence' command.  This is as hairy as it is 
  22. # because of the desire to exactly mimic ksh (whose behavior was determined
  23. # empirically).
  24. # This depends somewhat on knowing the format of the output of the bash
  25. # `builtin type' command.
  26. #
  27.  
  28. whence ()
  29. {
  30.   local vflag
  31.   local path
  32.  
  33.   vflag=
  34.   path=
  35.  
  36.   if [ "$#" = "0" ]; then
  37.     echo "whence: argument expected"
  38.     return 1
  39.   fi
  40.  
  41.   case "$1" in
  42.     -v) vflag=1
  43.       shift 1
  44.       ;;
  45.     -*) echo "whence: bad option: $1"
  46.       return 1
  47.       ;;
  48.     *) ;;
  49.   esac
  50.  
  51.   if [ "$#" = "0" ]; then
  52.     echo "whence: bad argument count"
  53.     return 1
  54.   fi
  55.  
  56.   for cmd; do
  57.     if [ "$vflag" ]; then
  58.       echo $(builtin type $cmd | sed 1q)
  59.     else
  60.       path=$(builtin type -path $cmd)
  61.  
  62.       if [ "$path" ]; then
  63.         echo $path
  64.       else
  65.         case "$cmd" in
  66.           /*) echo ""
  67.             ;;
  68.           *)
  69.             case "$(builtin type -type $cmd)" in
  70.               "") echo ""
  71.                 ;;
  72.               *) echo "$cmd"
  73.                 ;;
  74.             esac
  75.           ;;
  76.         esac
  77.       fi
  78.     fi
  79.   done
  80.   return 0
  81. }
  82.  
  83. #
  84. # For real ksh homeboy fanatics, redefine the `type' builtin with a ksh
  85. # version.
  86. #
  87. #type()
  88. #{
  89. #    whence -v "$*"
  90. #}
  91.  
  92. cd ()
  93. {
  94.   case $# in
  95.     0) builtin cd "$HOME" ;;
  96.     1) builtin cd "$@" ;;
  97.     2) old="$1"
  98.        new="$2"
  99.        dir=$(echo "$PWD" | sed "s:$old:$new:g")
  100.  
  101.        case "$dir" in
  102.          "$PWD") echo "bash: cd: bad substitution" >&2 ;;
  103.          *)      echo "$dir"
  104.                  builtin cd "$dir"
  105.            ;;
  106.        esac
  107.       ;;
  108.     *) echo "cd: wrong arg count" >&2 ;;
  109.   esac
  110. }
  111.  
  112. #
  113. # ksh print emulation
  114. #
  115. #    print [-Rnprsu[n]] [arg ...]
  116. #
  117. #    -    end of options
  118. #    -R    BSD-style -- only accept -n, no escapes
  119. #    -n    do not add trailing newline
  120. #    -p    no-op (no coprocesses)
  121. #    -r    no escapes
  122. #    -s    no-op (print to the history file)
  123. #    -u n    redirect output to fd n
  124. #
  125.  
  126. print()
  127. {
  128.   local eflag=-e
  129.   local nflag=
  130.   local fd=1
  131.  
  132.   OPTIND=1
  133.  
  134.   while getopts "Rnprsu:" c; do
  135.     case $c in
  136.       R|r) eflag= ;;
  137.       n)   nflag=-n ;;
  138.       u)   redir=">&$OPTARG"
  139.            fd=$OPTARG
  140.         ;;
  141.       p|s) ;;
  142.     esac
  143.   done
  144.  
  145.   shift $[ $OPTIND - 1 ]
  146.   echo $eflag $nflag "$@" >&$fd
  147. }
  148.